import os
import sys
import win32com.client
from PIL import Image, ImageDraw, ImageFont
from reportlab.pdfgen import canvas

# 将PPT转为图片
def ppt_to_images(ppt_path, output_dir):
    powerpoint = win32com.client.Dispatch("PowerPoint.Application")
    presentation = powerpoint.Presentations.Open(ppt_path)
    
    # 创建输出目录
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    
    slide_count = len(presentation.Slides)
    
    image_paths = []
    
    # 遍历每一页幻灯片并导出为图片
    for slide_index in range(slide_count):
        slide = presentation.Slides.Item(slide_index+1)
        image_path = os.path.join(output_dir, f"slide_{slide_index + 1}.png")
        slide.Export(image_path, "PNG")
        image_paths.append(image_path)
    
    presentation.Saved = True  # 标记文件为已保存，避免弹出保存提示
    presentation.Close()
    powerpoint.Quit()
    
    return image_paths

def add_image_watermark(image_path, watermark_path, output_path, opacity=0.3):
    # 打开主图片和水印图片
    original_image = Image.open(image_path).convert("RGBA")
    watermark_image = Image.open(watermark_path).convert("RGBA")

    # 调整水印透明度
    if opacity < 1.0:
        alpha = watermark_image.getchannel('A')
        alpha = alpha.point(lambda x: int(x * opacity))  # 确保结果为整数
        watermark_image.putalpha(alpha)

    # 获取主图和水印的宽高
    width, height = original_image.size
    watermark_width, watermark_height = watermark_image.size
    
    # 计算水印位置，使水印居中
    x_position = (width - watermark_width) // 2
    y_position = (height - watermark_height) // 2
    
    # 创建透明底图并粘贴水印
    transparent_base = Image.new("RGBA", original_image.size)
    transparent_base.paste(watermark_image, (x_position, y_position), mask=watermark_image)
    
    # 合并水印和原图
    result = Image.alpha_composite(original_image, transparent_base)

    # 转换为 RGB 模式以便保存为 PDF 或 JPEG
    final_image = result.convert("RGB")

    # 保存最终图片
    final_image.save(output_path)  # 补充保存图片的代码

# 将图片转为PDF
def images_to_pdf(image_paths, pdf_path):
    images = []
    for image_path in image_paths:
        image = Image.open(image_path)
        
        # 如果图片是RGBA，转换为RGB
        if image.mode == "RGBA":
            image = image.convert("RGB")
        
        images.append(image)
    
    # 保存多张图片为一个PDF，保持原尺寸
    images[0].save(pdf_path, "PDF", save_all=True, append_images=images[1:])


# 主函数
def ppt_to_pdf_with_watermark(ppt_path, pdf_path, watermark_text="Watermark", watermark_position=("bottom", "right"), opacity=0.3):
    # Step 1: 将PPT转为图片

    # 使用相对路径构建图片输出目录
    image_dir = os.path.join(base_dir, 'ppt_images')
    image_paths = ppt_to_images(ppt_path, image_dir)
    
    # Step 2: 给每张图片加水印
    for image_path in image_paths:
        add_image_watermark(image_path,watermark_path,image_path, opacity=opacity)
    
    # Step 3: 将图片转为PDF
    images_to_pdf(image_paths, pdf_path)

# 主函数，处理多个 PPT 文件
def convert_all_ppts_to_pdf_with_watermark(ppt_dir, pdf_dir, watermark_path, watermark_text="Watermark", watermark_position=("bottom", "right"), opacity=0.3):
    # 遍历 PPT 目录中的所有文件
    for filename in os.listdir(ppt_dir):
        if filename.endswith(('.pptx', '.ppt')):
            # 构建 PPT 文件的完整路径
            ppt_path = os.path.join(ppt_dir, filename)
            # 生成对应的 PDF 文件名
            pdf_filename = os.path.splitext(filename)[0] + '.pdf'
            # 构建 PDF 文件的完整路径
            pdf_path = os.path.join(pdf_dir, pdf_filename)
            # 调用转换函数处理单个 PPT 文件
            ppt_to_pdf_with_watermark(ppt_path, pdf_path, watermark_text, watermark_position, opacity)

# 获取当前脚本所在的目录
base_dir = os.path.dirname(os.path.abspath(__file__))

if getattr(sys, 'frozen', False):
       # 打包后：通过sys.executable或sys.argv[0]获取路径
       base_dir = os.path.dirname(sys.executable)
else:
    # 脚本模式：使用__file__获取当前脚本路径
    base_dir = os.path.dirname(os.path.abspath(__file__))

# 假设 PPT 源文件存放在 'ppt_files' 目录下
ppt_dir = os.path.join(base_dir, 'ppt_files')

# 假设生成的 PDF 存放在 'pdf_files' 目录下
pdf_dir = os.path.join(base_dir, 'pdf_files')

# 假设水印图片存放在 'watermark' 目录下
watermark_dir = os.path.join(base_dir, 'watermark')
watermark_filename = 'watermark.png'
watermark_path = os.path.join(watermark_dir, watermark_filename)

# 使用示例
convert_all_ppts_to_pdf_with_watermark(ppt_dir, pdf_dir, watermark_path, watermark_text="Confidential", opacity=0.4)
